<?php
?>

<div class="panel panel-default">
    <div class="panel-body">
        <div class="form-inline">
            <div class="btn-group col-lg-6">
                <a class="btn btn-default " type="button" data-toggle="modal" href="#addModal"><i class="fa fa-plus"></i> 新增授信</a>
            </div>

            <div class="pull-right">
                <div class="form-group">
                    <?php
                    if ($manager_institution_code == '0') {
                        ?>
                        <?= \app\widgets\InstitutionHierarchyAutocompleteWidget::widget(['id' => 'searchInstitution', 'defaultText' => $current_institution_name, 'currentInstitutionCode' => $current_institution_code]) ?>
                        <?php
                    } else {
                        ?>
                        <?= \app\widgets\InstitutionChildrenAutocompleteWidget::widget(['id' => 'searchInstitution', 'defaultText' => $current_institution_name, 'currentInstitutionCode' => $current_institution_code]) ?>
                        <?php
                    }
                    ?>
                </div>
                <div class="form-group">
                    <select class="form-control" id="searchCreditStatus">
                        <option value="0" <?= $current_credit_status == '0' ? 'selected' : '' ?>>----全部状态----</option>
                        <option value="1" <?= $current_credit_status == '1' ? 'selected' : '' ?>>已提交申请</option>
                        <option value="11" <?= $current_credit_status == '11' ? 'selected' : '' ?>>审核成功</option>
                        <option value="12" <?= $current_credit_status == '12' ? 'selected' : '' ?>>审核失败</option>
                        <option value="20" <?= $current_credit_status == '20' ? 'selected' : '' ?>>授信成功</option>
                        <option value="21" <?= $current_credit_status == '21' ? 'selected' : '' ?>>授信失败</option>
                        <option value="22" <?= $current_credit_status == '22' ? 'selected' : '' ?>>授信取消</option>
                    </select>
                </div>
                <div class="btn-group">
                    <button type="button" class="btn btn-primary" id="btnSearch">
                        <i class="fa fa-search"></i> 搜索
                    </button>
                </div>
            </div>
        </div>
    </div>
</div>

<div class="panel">
    <div class="panel-body">
        <div class="table-responsive">
            <table class="table table-bordered table-hover table-condensed">
                <thead class="nowrap">
                <tr>
                    <th>机构</th>
                    <th class="col-lg-1">授信额度</th>
                    <th class="col-lg-1">申请人</th>
                    <th class="col-lg-1">申请时间</th>
                    <th class="col-lg-1">状态</th>
                    <th class="col-lg-1">操作</th>
                </tr>
                </thead>
                <tbody>
                <?php
                foreach ($items as $item) {
                    ?>
                    <tr>
                        <td><?= $item['institution_name'] ?></td>
                        <td class="nowrap"><?= $item['credit_quota'] ?></td>
                        <td class="nowrap"><?= $item['apply_user_name'] ?></td>
                        <td class="nowrap"><?= $item['create_datetime'] ?></td>
                        <td class="nowrap">
                            <?php
                            switch ($item['status']) {
                                case 1:
                                    echo "<label class='label label-warning'>{$item['status_name']}</label>";
                                    break;
                                case 11:
                                    echo "<label class='label label-success'>{$item['status_name']}</label>";
                                    break;
                                case 12:
                                    echo "<label class='label label-danger'>{$item['status_name']}</label>";
                                    break;
                                default:
                                    echo $item['status_name'];
                                    break;
                            }
                            ?>
                        </td>
                        <td class="nowrap">
                            <div class="btn-group">
                                <a class="btn btn-link" data-toggle="modal" data-credit-id="<?= $item['id'] ?>" href="#detailModal">详情</a>
                            </div>
                        </td>
                    </tr>
                    <?php
                }
                ?>
                </tbody>
            </table>
        </div>
        <div class="pull-left">
            <?= \app\widgets\LinkPagerExt::widget(['pagination' => $pagination]) ?>
        </div>
    </div>
</div>

<div aria-hidden="true" role="dialog" tabindex="-1" id="detailModal" class="modal fade">
    <div class="modal-dialog">
        <div class="modal-content">
            <div class="modal-header">
                <button aria-hidden="true" data-dismiss="modal" class="close" type="button">×</button>
                <h4 class="modal-title">授信详情</h4>
            </div>
            <div class="modal-body" id="detail_content">
                正在加载数据....
            </div>
        </div>
    </div>
</div>

<div aria-hidden="true" role="dialog" tabindex="-1" id="addModal" class="modal fade">
    <div class="modal-dialog">
        <div class="modal-content">
            <div class="modal-header">
                <button aria-hidden="true" data-dismiss="modal" class="close" type="button">×</button>
                <h4 class="modal-title">新增授信</h4>
            </div>
            <div class="modal-body">
                <div class="form-horizontal">
                    <div class="form-group">
                        <label class="col-lg-3 col-sm-3 control-label">机构:</label>
                        <div class="col-lg-9">
                            <?php
                            if ($manager_institution_code == '0') {
                                ?>
                                <?= \app\widgets\InstitutionHierarchyAutocompleteWidget::widget(['id' => 'inputInstitution', 'defaultText' => $current_institution_name]) ?>
                                <?php
                            } else {
                                ?>
                                <?= \app\widgets\InstitutionChildrenAutocompleteWidget::widget(['id' => 'inputInstitution', 'defaultText' => $current_institution_name]) ?>
                                <?php
                            }
                            ?>
                        </div>
                    </div>
                    <div class="form-group">
                        <label class="col-lg-3 col-sm-3 control-label">申请人:</label>
                        <div class="col-lg-9">
                            <input type="text" class="form-control" id="inputApplyUserName"/>
                        </div>
                    </div>
                    <div class="form-group">
                        <label class="col-lg-3 col-sm-3 control-label">授信额度:</label>
                        <div class="col-lg-9">
                            <div class="input-group m-bot15">
                                <span class="input-group-addon">￥</span>
                                <input type="number" min="0" class="form-control" id="inputCreditQuota">
                                <span class="input-group-addon ">.00</span>
                            </div>
                        </div>
                    </div>
                    <div class="form-group">
                        <label class="col-sm-3 control-label col-lg-3">备注:</label>
                        <div class="col-lg-9">
                            <textarea rows="7" class="form-control" id="inputRemark"></textarea>
                        </div>
                    </div>
                </div>
            </div>
            <div class="modal-footer">
                <button class="btn btn-default" data-dismiss="modal">取消</button>
                <button class="btn btn-primary" id="btnCreateCreditOk">确定</button>
            </div>
        </div>
    </div>
</div>

<script type="application/javascript">
    (function () {
        $('#btnCreateCreditOk').click(function () {
            var data = {};
            data.institution_code = $('#inputInstitution').val();
            if (!data.institution_code) {
                alert('请选择授信机构!');
                return;
            }
            data.apply_user_name = $('#inputApplyUserName').val();
            data.credit_quota = $('#inputCreditQuota').val();
            if (data.credit_quota==null || data.credit_quota=='') {
                alert('请输入授信额度!');
                return;
            }else if (data.credit_quota<=0) {
                alert('授信额度必须大于0!');
                return;
            }
            data.remark = $('#inputRemark').val();
            requestJson('add-credit-handler', data, true, function (responseData) {
                $('#addModal').modal('hide');
                if (responseData.status == 0) {
                    window.location.reload();
                } else {
                    alert(responseData.message);
                }
            });
        });

        $('#detailModal').on('show.bs.modal', function (e) {
            $("#detail_content").html('正在加载数据....');
            var creditId = e.relatedTarget.attributes['data-credit-id'].value;
            requestHtml('credit-detail-partview', {credit_id: creditId}, true, function (responseHtml) {
                $("#detail_content").html(responseHtml);
            });
        });

        $('#btnSearch').click(function () {
            var searchInstitutionCode = $('#searchInstitution').val();
            var searchCreditStatus = $('#searchCreditStatus').val();
            var url = window.location.href;
            if (searchInstitutionCode) {
                url = setUrlParam(url, 'institution_code', searchInstitutionCode);
            } else {
                url = removeUrlParam(url, 'institution_code');
            }
            url = setUrlParam(url, 'credit_status', searchCreditStatus);
            url = setUrlParam(url, 'page', 1);
            window.location.href = url;
        });
    })(jQuery);

    //设置模态窗:当用户点击模态框外部时不会关闭、当初始化时不显示模态框。
    $(function () {
        $('#addModal').modal({
            backdrop: 'static', show: false
        });
    });
</script>

